
namespace HooIzDat.Web.Models
{
    public partial class GameTile
    {
        public bool CanGuess(Player currentPlayer)
        {
            if (Game == null) return false;

            if (Game.GameState.ActivePlayer == currentPlayer)
            {
                return Game.IsWaitingForQuestion && this.IsOpen;
            }
            else
            {
                return false;
            }
        }

        public bool CanChangeState(Player currentPlayer)
        {
            if (Game == null) return false;

            if (Game.GameState.ActivePlayer == currentPlayer)
            {
                return Game.IsWaitingForQuestion && !this.IsOpen ||
                       Game.IsWaitingForNextTurn;
            }
            else
            {
                return false;
            }
        }
    
        public virtual int GameID
        {
            get { return _gameID; }
            set
            {
                if (_gameID != value)
                {
                    if (Game != null && Game.GameID != value)
                    {
                        Game = null;
                    }
                    _gameID = value;
                }
            }
        }
        private int _gameID;
    
        public virtual int PlayerID
        {
            get { return _playerID; }
            set
            {
                if (_playerID != value)
                {
                    if (Player != null && Player.PlayerID != value)
                    {
                        Player = null;
                    }
                    _playerID = value;
                }
            }
        }
        private int _playerID;
    
        public virtual int CharacterID
        {
            get { return _characterID; }
            set
            {
                if (_characterID != value)
                {
                    if (Character != null && Character.CharacterID != value)
                    {
                        Character = null;
                    }
                    _characterID = value;
                }
            }
        }
        private int _characterID;
    
        public virtual bool IsOpen
        {
            get;
            set;
        }
    
        public virtual int TileIndex
        {
            get;
            set;
        }

        public virtual Character Character
        {
            get { return _character; }
            set
            {
                if (!ReferenceEquals(_character, value))
                {
                    var previousValue = _character;
                    _character = value;
                    FixupCharacter(previousValue);
                }
            }
        }
        private Character _character;
    
        public virtual Game Game
        {
            get { return _game; }
            set
            {
                if (!ReferenceEquals(_game, value))
                {
                    var previousValue = _game;
                    _game = value;
                    FixupGame(previousValue);
                }
            }
        }
        private Game _game;
    
        public virtual Player Player
        {
            get { return _player; }
            set
            {
                if (!ReferenceEquals(_player, value))
                {
                    var previousValue = _player;
                    _player = value;
                    FixupPlayer(previousValue);
                }
            }
        }
        private Player _player;

        private void FixupCharacter(Character previousValue)
        {
            if (previousValue != null && previousValue.GameTiles.Contains(this))
            {
                previousValue.GameTiles.Remove(this);
            }
    
            if (Character != null)
            {
                if (!Character.GameTiles.Contains(this))
                {
                    Character.GameTiles.Add(this);
                }
                if (CharacterID != Character.CharacterID)
                {
                    CharacterID = Character.CharacterID;
                }
            }
        }
    
        private void FixupGame(Game previousValue)
        {
            if (previousValue != null && previousValue.Tiles.Contains(this))
            {
                previousValue.Tiles.Remove(this);
            }
    
            if (Game != null)
            {
                if (!Game.Tiles.Contains(this))
                {
                    Game.Tiles.Add(this);
                }
                if (GameID != Game.GameID)
                {
                    GameID = Game.GameID;
                }
            }
        }
    
        private void FixupPlayer(Player previousValue)
        {
            if (previousValue != null && previousValue.GameTiles.Contains(this))
            {
                previousValue.GameTiles.Remove(this);
            }
    
            if (Player != null)
            {
                if (!Player.GameTiles.Contains(this))
                {
                    Player.GameTiles.Add(this);
                }
                if (PlayerID != Player.PlayerID)
                {
                    PlayerID = Player.PlayerID;
                }
            }
        }
    }
}
